﻿using BLL;
using DataAccess;
using DevExpress.XtraEditors;
using NLog;
using QuanLyKhachSan.Validation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyKhachSan
{
    public partial class FormDoiMatKhau : XtraForm
    {
        protected static Logger _logger = LogManager.GetCurrentClassLogger();
        private string ktramatkhau;
        public TaiKhoan taiKhoan = new TaiKhoan();
        public bool IsInserted = false;
        private BaoMatManager bmManager = new BaoMatManager();

        public FormDoiMatKhau()
        {
            InitializeComponent();
        }

        public FormDoiMatKhau(TaiKhoan tk)
        {
            InitializeComponent();
            InitTaiKhoan(tk);
        }

        private void InitTaiKhoan(TaiKhoan tk)
        {
            txtUsername.Text = tk.Username;
            ktramatkhau = tk.Password;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnDoiMatKhau_Click(object sender, EventArgs e)
        {
            if ((ktramatkhau == txtPassword.Text) && (txtPasswordMoi.Text == txtNhapLaiPassword.Text))
            {
                _logger.Info("Chọn thao tác đổi mật khẩu.");
                taiKhoan.Username = txtUsername.Text;
                taiKhoan.Password = txtNhapLaiPassword.Text;
                ValidateResult vResult = bmManager.Validate(taiKhoan);
                if (!vResult.Message.Any())
                {
                    ActionResult aResult = bmManager.CapNhat(taiKhoan);
                    if (!aResult.Exceptions.Any())
                    {
                        IsInserted = true;
                        Close();
                    }
                    else
                    {
                        string s = "";
                        foreach (var ex in aResult.Exceptions)
                        {
                            s += ex.Message + "\n";
                        }
                        XtraMessageBox.Show(s);
                    }
                }
                else
                {
                    string s = "";
                    foreach (var ex in vResult.Message)
                    {
                        s += ex + "\n";
                    }
                    XtraMessageBox.Show(s);
                }
            }
            else
            {
                XtraMessageBox.Show("Vui lòng điền đầy đủ thông tin!");
            }
            
        }

        private void FormDoiMatKhau_Load(object sender, EventArgs e)
        {
            txtPassword.Text = "";
            txtPasswordMoi.Text = "";
            txtNhapLaiPassword.Text = "";
        }

    }
}
